package com.jike.noobmoney.mvp.view.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.ComUtils;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.toomee.mengplus.manager.TooMeeManager;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2019/3/9.
 * 签到  1.获取余额 2.签到
 */

public class QianDaoActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_money)
    TextView tvMoney;

    @BindView(R.id.tv_check)
    TextView tvCheck;
    @BindView(R.id.ll_check_in)
    View llCheckIn;
    @BindView(R.id.tv_check_in)
    TextView tvCheckIn;
    @BindView(R.id.iv_check)
    ImageView ivCheck;

    @BindView(R.id.tv_yaoqing)
    TextView tvYaoQing;
    @BindView(R.id.tv_push)
    TextView tvPush;

    private UserPresenter userPresenter;
    private String userid;

    @Override
    public Boolean isfits() {
        return false;
    }

    @Override
    protected int layoutResID() {
        return R.layout.activity_qiandao;
    }

    private long Toal = 60 * 60 * 1000;

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        refreshData();

        String lastTime = SPUtils.getInstance().getString(ConstantValue.SpType.day, "2019-03-11");
        String nowTime = ComUtils.getDay();
        if (nowTime.equals(lastTime)) {
            changeState(false);  //同一天 不可签到
        } else {
            changeState(true);   //不同天 可以签到
        }

        tvYaoQing.setSelected(true);
        tvPush.setSelected(true);
    }


    /**
     * 获取余额
     */
    private void refreshData() {
        showProgress();
        userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
    }

    /**
     * 签到
     */
    private void checkIn() {
        showProgress();
        userPresenter.checkin(userid, ConstantValue.RequestKey.checkIn);

    }

    @OnClick({R.id.ll_check_in, R.id.tv_check_in, R.id.tv_yaoqing, R.id.tv_push
            , R.id.iv_guanfang, R.id.iv_yaoqing, R.id.iv_renwu, R.id.iv_youxi
            , R.id.iv_video, R.id.iv_qqqun, R.id.iv_gzh
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_check_in:
                if (llCheckIn.isSelected()) {
                    checkIn();
                } else {
                    ToastUtils.showShortToastSafe("今天已经签到了,明天再来吧~");
                }
                break;
            case R.id.tv_check_in:
                if (tvCheck.isSelected()) {
                    checkIn();
                }
                break;
            case R.id.tv_yaoqing:
                recommend();
                break;
            case R.id.tv_push:
                startActivity(new Intent(this, PushTaskActivity.class));
                break;

            case R.id.iv_guanfang:
                //官方推荐
                startActivity(new Intent(this, HotTopActivity.class));
                break;
            case R.id.iv_yaoqing:
                //邀请页面
                startActivity(new Intent(this, YaoqingActivity.class));
                break;
            case R.id.iv_renwu:
                //任务
                //setResult(RESULT_OK);
                startActivity(new Intent(this,TastListActivity.class));
                finish();
                break;
            case R.id.iv_youxi:
                //游戏
                TooMeeManager.start(context);
                break;
            case R.id.iv_video:
                //新手教程voide
                Intent intent = new Intent(context, VideoTwoActivity.class);
                intent.putExtra("isTask", false);
                startActivity(intent);
                break;
            case R.id.iv_qqqun:
                //QQ群
                ComUtils.startQQ(context, 3, "564104407");
                break;
            case R.id.iv_gzh:
                //GuanzhuActivity.startAct(this, 3);
                Intent intent2 = new Intent(context, ExchangeActivity.class);
                startActivity(intent2);
                break;

        }
    }

    /**
     * 点击分享
     */
    public void recommend() {
        String recode = SPUtils.getInstance().getString(ConstantValue.SpType.recode);//签到页
        String taskurl = SPUtils.getInstance().getString(ConstantValue.SpType.taskurl);
        String url = !TextUtils.isEmpty(taskurl) ? taskurl + "?recode=" + recode : ConstantValue.COMMENTURLCOPY + recode;
        UMWeb web = new UMWeb(url);
        web.setTitle("小白赚钱");
        web.setThumb(new UMImage(context, R.mipmap.ic_launcher));
        web.setDescription(getString(R.string.text_yaoqing1));
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(web);
        shareAction.setDisplayList(SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        shareAction.open();
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            MoneyEntity.UserBean userBean = (MoneyEntity.UserBean) data;
            tvMoney.setText("" + userBean.getMoney());
        }

        if (ConstantValue.RequestKey.checkIn.equals(actionType)) {
            ToastUtils.showShortToastSafe("" + msg);
            //签到成功刷新余额
            refreshData();

            long checkinTime = System.currentTimeMillis();
            SPUtils.getInstance().put("checkin_time", checkinTime);

            SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
            changeState(false); //签到成功
        }

    }

    private void changeState(boolean isAbleQiandao) {
        if (isAbleQiandao) {
            //可以签到
            llCheckIn.setSelected(true);
            ivCheck.setVisibility(View.VISIBLE);
            tvCheck.setText("签到");

            tvCheckIn.setSelected(true);
            tvCheckIn.setText("去签到");
        } else {
            //不可签到
            llCheckIn.setSelected(false);
            ivCheck.setVisibility(View.GONE);
            tvCheck.setText("已签到");

            tvCheckIn.setSelected(false);
            tvCheckIn.setText("已签到");
        }

    }


    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);
        if ("200".equals(code)) {
            SPUtils.getInstance().put(ConstantValue.SpType.day, ComUtils.getDay());
            changeState(false);
        }
    }
}
